var MAX_PRICE = 1000000000000;
var MIN_PRICE = 10000;
function validateForm($form){
	$form.validate({
		 ignore: "",
		// focusInvalid : true,
        // onkeyup : true,
        // onfocusout : true,
        // onsubmit: true,
        //debug: true,
        onfocusout : function( element, event ) {
		    if ($(element).valid()){
        		$(element).closest('div.posting_row').find('.category_group_warntip').removeClass("show");
		    	$(element).closest('div.posting_row').find('.category_group_warntip').addClass("hide");
		    }
		},
        onkeyup: function( element, event ) {
		    $(element).valid();
		},
		
        errorPlacement: function (error, element) {
            $(element).closest('div.posting_row').find('.category_group_warntip').removeClass("hide");
            $(element).closest('div.posting_row').find('.category_group_warntip').addClass("show");
        },
        invalidHandler: function(form, validator) {
            if (!validator.numberOfInvalids()){
                return;
            }
            scrollToTopElement($(validator.errorList[0].element));
        }
    });
    
}
function initRules(){
	$('#data_title').rules("add", {
            required : true,
            minlength: 2,
      });
    $('#data_category_id').rules("add", {
            validateSelect : "0"
     });
 	$('#data_region_id').rules("add", {
        validateSelect : "0"
    });
    $('#data_price').rules("add", {
            required : true,
            checkRange: true
    });
    $("#data_product_year").rules("add", {
            required : true,
            number: true
    });
    $("#data_product_country").rules("add", {
            required : true
    });
    $("#data_sell_name").rules("add", {
            required : true
    });
    $("#data_phone_number").rules("add", {
            required : true,
            number: true
    });
    $("#data_mail_address").rules("add", {
            required : true,
            email: true
    });
    $("#data_time_used").rules("add", {
            required : true,
            number: true
    });
    $(".postImage").rules("add", {
            requireImg : true
    });
}
function scrollToTopElement($element){
	if($element.hasClass('postImage')){
		$element = $('.element-images');
	}
	//posting_element
    $('html, body').animate({
        scrollTop: $element.offset().top
    }, 100);
}
/**
 * add rule
 */
 function addCustomRule(){
 	 $.validator.addMethod('validateSelect', function (value, element, arg) {
        return arg != value;
    }, 'Vui lòng chọn');
    $.validator.addMethod('requireImg', function (value, element, arg) {
        var n = 0;
		$(".hdImg").each(function(){
		  if($(this).val()!=""){
		     n++;
		  }
		});
		return n > 0;
    }, 'Vui lòng chọn');
    $.validator.addMethod('checkRange', function (value, element, arg) {
    	value = value.split(".");
    	if(value.length>0){
    		value = value[0];
    	}
    	value = value.replace(/[.,]/g, '');
    	if(isNaN(value)){
    		return false;
    	}
    	value = eval(value);
    	return value > MIN_PRICE && value < MAX_PRICE;
    	
    },'Nhập số');
 }
/**
 *function validate 
 */
function checkSelectValue(value){
	return value !="0";
}
